<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleEdit"
          :disabled="single"
          v-hasPermi="['produce:mxCourse:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-position"
          size="mini"
          :disabled="multiple"
          @click="confirmMof"
          v-hasPermi="['produce:mxCourse:confirm']"
        >
          确认
        </el-button>
      </el-col>
    </el-row>

    <el-table
      highlight-current-row
      v-loading="loading"
      :data="mofMstrList"
      border
      stripe
      :height="tableH"
      ref="clickTable"
      @row-click="clickRow"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="制造单号"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="mofZzdh"
      />
      <el-table-column
        label="销售单号"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="mofSodh"
      />
      <el-table-column
        label="物料代码"
        :show-overflow-tooltip="true"
        min-width="20%"
        align="center"
        prop="mofWldm"
      />
      <el-table-column
        label="品名规格"
        :show-overflow-tooltip="true"
        min-width="20%"
        align="center"
        prop="mofPmgg"
      />
      <el-table-column
        label="计划数量"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="mofZzsl"
      />
      <el-table-column
        label="完工日期"
        :show-overflow-tooltip="true"
        min-width="20%"
        align="center"
        prop="mofWgrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.mofWgrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="模具编号"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="mofMjbh"
      />
      <el-table-column
        label="模具状态"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="mofMoldStatus"
      >
        <template #default="scope">
          <dict-tag
            :options="mouldStatusOptions"
            :value="scope.row.mofMoldStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="穴位"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="mofXs"
      />
      <el-table-column
        label="成型时间"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="mofCxsj"
      />
      <el-table-column
        label="预计工时"
        :show-overflow-tooltip="true"
        min-width="10%"
        align="center"
        prop="mofEstimatedWorkingHours"
      />
    </el-table>
    <!-- 修改 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="700px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="13">
          <el-form-item label="制造单号" prop="mof_zzdh">
            <el-input v-model="form.mof_zzdh" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="模具状态" prop="mjm_mjzt">
            <el-select
              v-model="form.mjm_mjzt"
              placeholder="请选择模具状态"
              clearable
            >
              <el-option
                v-for="dict in mouldStatusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="模具编号" prop="mjm_mjbh">
            <el-select
              v-model="form.mjm_mjbh"
              placeholder="请选择模具"
              @change="changeSelect"
            >
              <el-option
                v-for="dict in mjmList"
                :key="dict.mjm_mjbh"
                :label="dict.mjm_mjbh"
                :value="dict.mjm_mjbh"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="14">
          <el-form-item label="模具名称" prop="mjm_mjmc">
            <el-input v-model="form.mjm_mjmc" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模具穴数" prop="mjm_xs">
            <el-input v-model="form.mjm_xs" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="成型时间" prop="mjm_cxsj">
            <el-input v-model="form.mjm_cxsj" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="物料代码" prop="mof_wldm">
            <el-input v-model="form.mof_wldm" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="14">
          <el-form-item label="品名规格" prop="mof_pmgg">
            <el-input v-model="form.mof_pmgg" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="itd_xs" label-width="90px">
            <span slot="label">
              <el-tooltip
                content="产品穴数、加工人数,请在<模具对应物料信息>功能下修改"
                placement="top"
              >
                <i class="el-icon-question"></i>
              </el-tooltip>
              产品穴数
            </span>
            <el-input v-model="form.itd_xs" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="加工人数" prop="itd_jgrs">
            <el-input v-model="form.itd_jgrs" disabled />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMofConfirm,
  getMofMjmByZzdh,
  updateMof,
  confirmMof,
} from "@/api/produce/materialScheduling";

export default {
  name: "MxCourse",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //模具状态字典
      mouldStatusOptions: [],
      // 非单个禁用
      single: true,
      /** 非多个禁用 */
      multiple: true,
      // 总条数
      total: 0,
      // 明细计划自动排程list
      mofMstrList: [],
      //表格高度
      tableH: document.body.clientHeight - 230,
      //选中的数组
      currentData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mofQrflag: 0,
      },
      //修改传递的制造单号
      cMofZzdh: null,

      // 表单参数
      form: {},
      //模具list
      mjmList: [],
      // 表单校验
      rules: {
        mjm_mjzt: [
          { required: true, message: "模具状态不能为空", trigger: "blur" },
        ],
        mjm_xs: [
          { required: true, message: "模具穴数不能为空", trigger: "blur" },
        ],
        mjm_cxsj: [
          { required: true, message: "成型时间不能为空", trigger: "blur" },
        ],
        mjm_mjbh: [
          { required: true, message: "模具编号不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getDicts("mould_status").then((response) => {
      this.mouldStatusOptions = response.data;
    });
    this.getList();
  },
  methods: {
    //下拉
    changeSelect(el) {
      for (let i of this.mjmList) {
        if (el == i.mjm_mjbh) {
          this.form.mjm_mjmc = i.mjm_mjmc;
          this.form.mjm_xs = i.mjm_xs;
          this.form.mjm_cxsj = i.mjm_cxsj;
          this.form.itd_xs = i.itd_xs;
          this.form.itd_jgrs = i.itd_jgrs;
        }
      }
    },
    /** 查询营销订单维护列表 */
    getList(row) {
      this.tableH = document.body.clientHeight - 280;
      this.loading = true;
      listMofConfirm(this.queryParams).then((response) => {
        this.mofMstrList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.cMofZzdh = null;
      this.form = {
        mof_id: null,
        mof_zzdh: null,
        mjm_mjzt: "ZK",
        mjm_mjbh: null,
        mjm_mjmc: null,
        mjm_xs: null,
        mjm_cxsj: null,
        itd_xs: null,
        itd_jgrs: null,
        mof_wldm: null,
        mof_pmgg: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    clickRow(row) {
      this.$refs.clickTable.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.currentData = selection;
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    //修改
    handleEdit() {
      this.open = true;
      this.title = "计划确认-修改";
      this.reset();
      this.cMofZzdh = this.currentData[0].mofZzdh;
      getMofMjmByZzdh(this.cMofZzdh).then((res) => {
        this.form = res.data.data;
        this.mjmList = res.data.mjmList;
      });
    },
    //修改-确认
    submitForm() {
      if (
        this.form.mjm_xs > 0 &&
        this.form.mjm_xs != null &&
        this.form.mjm_cxsj > 0 &&
        this.form.mjm_cxsj != null
      ) {
        const data = {
          mofId: this.form.mof_id,
          mofMoldStatus: this.form.mjm_mjzt,
          mofMjbh: this.form.mjm_mjbh,
          mofXs: this.form.mjm_xs,
          mofCxsj: this.form.mjm_cxsj,
        };
        updateMof(data)
          .then((res) => {
            this.msgSuccess("修改成功");
            this.getList();
            this.reset();
            this.open = false;
          })
          .catch(() => {
            this.msgError("修改成功");
          });
      } else {
        this.msgError("穴数或成型时间不能为空！");
      }
    },
    isCharm() {
      for (let i of this.currentData) {
        if (
          i.mofMoldStatus == "WX" ||
          i.mofMoldStatus == "BF" ||
          i.mofMoldStatus == "WF" ||
          i.mofMoldStatus == ""
        ) {
          this.msgError(
            "制造单号: < " +
              i.mofZzdh +
              " > 的模具状态:<" +
              i.mofMoldStatus +
              ">不能用于生产,请核对"
          );
          return true;
        }
        if (i.mofMjbh == "" || i.mofMjbh == null) {
          this.msgError("制造单号: < " + i.mofZzdh + " > 模具未绑定,请核对");
          return true;
        }
      }
      return false;
    },
    //明细计划-确认
    confirmMof() {
      if (this.currentData.length > 0) {
        if (this.isCharm()) {
        } else {
          let ids = this.currentData.map((item) => item.mofId);
          this.$confirm("是否确认提交选中的单据?", "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(function () {
              return confirmMof(ids);
            })
            .then(() => {
              this.getList();
              this.msgSuccess("成功");
            })
            .catch(() => {});
        }
      } else {
        this.msgError("未找到有效数据,请重新勾选数据");
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
